const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    backgroundcolor:"#00a92c",
    wuid:0,
    ywd:1, //伪装审核
    upid:"", // 父级id
    mobile:"",
    isLogin: false,
    headerImgPath: "",
    userInfo: {}, // 用户信息
    orderCount: {}, // 订单数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 设置navbar标题、颜色
    App.setNavigationBar();
    var  color=wx.getStorageSync('wxapp');
    console.log(color.top_background_color)
    let _this = this;
    _this.setData({
      ywd:options.ywd,
      backgroundcolor: color.top_background_color,   //此处设置颜色后，会把background-image渲染掉
      // phone:color.navbar.phone
    });
  },

  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this;
    let _upid = wx.getStorageSync('referee_id');
    if(_upid>0){
      //_this.setData({upid:'_'+_upid});    //如果用户存在父级id，则显示父级id，目前没必要，所以注销
    }
    // console.log('get xiaoqu uid:', wx.getStorageSync('uid'))
    _this.setData({
      mobile: wx.getStorageSync('true_mobile'),
      wuid: wx.getStorageSync('uid') || 'XXX',
      isLogin: App.checkIsLogin()
    });
    // 获取当前用户信息
    _this.getUserDetail();
    App.pageOnLoad(this);
    //设置tabbar购物车总数量
    App.setBuyCarNumFun()
  },
  /**
   * 获取当前用户信息
   */
  getUserDetail() {
    let _this = this;
    App._get('user.index/detail', {mallID:App.uniacid}, function(result) {
      // 设置上传到服务器的用户头像
      if(result.data.userInfo){
        if(result.data.userInfo.avatarUrl.indexOf('http')<0){
          _this.setData({headerImgPath:App.YU+result.data.userInfo.avatarUrl});
        }
        else{
          _this.setData({headerImgPath:result.data.userInfo.avatarUrl});
        }
      }
      // 计算菜单的个数
      var len = 0;
      for(var obj in result.data.menus){
        len++
      }
      console.log('用户数据详情', result.data);
      //缓存是否伪装审车
      wx.setStorageSync('ywd', result.data.setting.ywd);
      _this.setData(result.data);
      _this.setData({menusLen:len});
    });
  },
  /**
   * 订单导航跳转
   */
  onTargetOrder(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    // 记录formid
    App.saveFormId(e.detail.formId);
    let urls = {
      all: '/pages/order/index?type=all',
      payment: '/pages/order/index?type=payment',
      delivery: '/pages/order/index?type=delivery',
      received: '/pages/order/index?type=received',
      refund: '/pages/order/refund/index',
    };
    // 转跳指定的页面
    wx.navigateTo({
      url: urls[e.currentTarget.dataset.type]
    })
  },

  /**
   * 菜单列表导航跳转
   */
  onTargetMenus(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    // 记录formId
    App.saveFormId(e.detail.formId);
    wx.navigateTo({
      url: '/' + e.currentTarget.dataset.url
    })
  },

  /**
   * 跳转我的钱包页面
   */
  onTargetWallet(e) {
    let _this = this;
    let _ywd = e.currentTarget.dataset.ywd
    // console.log('this is my ywd:::'+_ywd)
    if (!_this.onCheckLogin()) {
      return false;
    }
    // 记录formId
    App.saveFormId(e.detail.formId);
    wx.navigateTo({
      url: './wallet/index?ywd='+_ywd
    })
  },

  onTargetWallet22(e) {
    let _this = this;
    let _ywd = e.currentTarget.dataset.ywd
    // console.log('this is my ywd:::'+_ywd)
    if (!_this.onCheckLogin()) {
      return false;
    }
    // 记录formId
    App.saveFormId(e.detail.formId);
    wx.navigateTo({
      url: './wallet/balance/log?ywd='+_ywd
    })
  },

  /**
   * 跳转积分明细页
   */
  onTargetPoints(e) {
    let _this = this;
    if (!_this.onCheckLogin()) {
      return false;
    }
    // 记录formId
    App.saveFormId(e.detail.formId);
    wx.navigateTo({
      url: '../points/log/index'
    });
  },
  // 跳转到缴费列表
  jumpJiaofeiOrder(){
    wx.navigateTo({
        url: '/pages/order-jiaofei/index'
    });
  },
  /**
   * 跳转到登录页
   */
  onLogin() {
    // wx.navigateTo({
    //   url: '../login/login',
    // });
    App.doLogin();
  },

  /**
   * 验证是否已登录
   */
  onCheckLogin() {
    let _this = this;
    if (!_this.data.isLogin) {
      App.showError('很抱歉，您还没有登录');
      return false;
    }
    return true;
  },

})